package org.bulu.project.system.service.impl;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.netty.util.internal.StringUtil;
import org.bulu.project.system.mapper.DanmuMapper;
import org.bulu.project.system.model.entity.DanMu;
import org.bulu.project.system.service.DanMuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DanMuServiceImpl implements DanMuService {

    @Autowired
    private DanmuMapper danmuMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获取弹幕列表
     * @param videoId
     * @param startTime
     * @param endTime
     * @return 弹幕列表
     *
     * 先查redis缓存 ，没有再查数据库然后把查询的写到redis
     */
    @Override
    public List<DanMu> getDanmus(Long videoId, String startTime, String endTime) {
        String key = "danmu_video_" + videoId;
        String value = redisTemplate.opsForValue().get(key);
        List<DanMu> list;
        if(!StringUtil.isNullOrEmpty(value)){
            list = JSONArray.parseArray(value, DanMu.class);
            if(!StringUtil.isNullOrEmpty(startTime) && !StringUtil.isNullOrEmpty(endTime)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startDate = null;
                Date endDate = null;
                try {
                    startDate = sdf.parse(startTime);
                    endDate = sdf.parse(endTime);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                List<DanMu> childList = new ArrayList<>();
                for(DanMu danmu : list){
                    Date createTime = danmu.getCreateDate();
                    if(createTime.after(startDate) && createTime.before(endDate)){
                        childList.add(danmu);
                    }
                }
                list = childList;
            }
        }else{
            Map<String, Object> params = new HashMap<>();
            params.put("videoId", videoId);
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            list = danmuMapper.getDanmus(params);
            //保存弹幕到redis
            redisTemplate.opsForValue().set(key, JSONObject.toJSONString(list));
        }
        return list;
    }

    /**
     * 弹幕加入数据库
     * @param danmu
     */
    @Async
    public void addDanmu(DanMu danmu) {
        danmuMapper.addDanmu(danmu);
    }
    /**
     * 把弹幕加入redis里
     * @param danmu
     */
    public void addDanmusToRedis(DanMu danmu) {
        String key = "danmu_video_" + danmu.getVid();
        String value = redisTemplate.opsForValue().get(key);
        //没有就创建一个新的
        List<DanMu> list = new ArrayList<>();
        if(!StringUtil.isNullOrEmpty(value)){
            list = JSONArray.parseArray(value, DanMu.class);
        }
        list.add(danmu);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(list));
    }
}
